﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Website.Kunde
{
    public partial class Daeklog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                if (Request.QueryString["TireSetId"] != null)
	            {
                    BindTireSetData(db, Convert.ToInt32(Request.QueryString["TireSetId"]));
                }
                else
                {
                    Response.Redirect("~/Kunde/Daek.aspx");
                }
            }
        }

        private void BindTireSetData(TireServiceDB db, int TireSetId)
        {
            gvTireSetLog.DataSource = db.GetTireSetLog(TireSetId).Where(p => p.LeftFrontTirePattern != null).OrderBy(p => p.LogDate);
            gvTireSetLog.DataBind();
        }

        #region GridView Functions

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvTireSetLog_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            // Format UnBoundColumn FromTireSetStatusDescription: Show from status description
            if (e.Column.FieldName == "FromTireSetStatusDescription")
            {
                // Get fromstatusid
                int fromstatusid = Convert.ToInt32(e.GetListSourceFieldValue("FromTireSetStatusId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                // Change field value
                if (fromstatusid != 0)
                {
                    e.Value = db.GetTireSetStatusDescription(fromstatusid);
                }
                else
                {
                    e.Value = "";
                }

                db.Dispose();
            }

            // Format UnBoundColumn ToTireSetStatusDescription: Show to status description
            if (e.Column.FieldName == "ToTireSetStatusDescription")
            {
                // Get tostatusid
                int tostatusid = Convert.ToInt32(e.GetListSourceFieldValue("ToTireSetStatusId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                e.Value = db.GetTireSetStatusDescription(tostatusid);

                db.Dispose();
            }
        }

        #endregion
    }
}